package org.wenhuayuan.project.common.view

import android.os.Bundle
import android.view.View
import android.widget.ProgressBar
import androidx.annotation.CallSuper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.fragment_list.*
import org.wenhuayuan.project.common.R
import org.wenhuayuan.project.common.uicomp.HiBaseFragment
import org.wenhuayuan.uilibrary.hiitem.HiAdapter
import org.wenhuayuan.uilibrary.hiitem.HiDataItem
import org.wenhuayuan.uilibrary.refresh.HiOverView
import org.wenhuayuan.uilibrary.refresh.HiRefresh
import org.wenhuayuan.uilibrary.refresh.HiRefreshLayout
import org.wenhuayuan.uilibrary.refresh.HiTextOverView

open class HiAbsListFragment : HiBaseFragment(), HiRefresh.HiRefreshListener {
    var pageIndex: Int = 1
    private lateinit var refreshHeaderView: HiTextOverView
    private var fragmentRecyclerView:HiRecyclerView?=null
    private var fragmentEmptyView:EmpetyView? = null
    private var fragmentLoadingProgressBar:ProgressBar? = null
    private var fragmentRefreshLayout: HiRefreshLayout? = null
    private var layoutManager:RecyclerView.LayoutManager? = null
    private var hiAdapter:HiAdapter? = null
    override fun getLayoutRes(): Int {
        return R.layout.fragment_list
    }

    companion object {
        const val PREFETCH_SIZE = 5
    }

    // @CallSuper 意思是不能覆盖我这里边的逻辑
    @CallSuper
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        fragmentRecyclerView = fragment_recycler_view
        fragmentEmptyView = fragment_empty_view
        fragmentLoadingProgressBar = fragment_loading_progressBar
        fragmentRefreshLayout = fragment_refresh_layout

        refreshHeaderView = HiTextOverView(context!!)
        fragmentRefreshLayout?.setRefreshOverView(refreshHeaderView)
        fragmentRefreshLayout?.setRefreshListener(this)

        layoutManager = createLayoutManager()
        hiAdapter = HiAdapter(context!!)

        fragmentRecyclerView?.layoutManager = layoutManager
        fragmentRecyclerView?.adapter = hiAdapter

        fragmentEmptyView?.visibility = View.GONE
        fragmentEmptyView?.setIcon(R.string.if_empty2)
        fragmentEmptyView?.setButton(getString(R.string.empty_action), View.OnClickListener {
            onRefresh()
        })
        fragmentLoadingProgressBar?.visibility = View.VISIBLE
        pageIndex = 1
    }

    open fun createLayoutManager() : RecyclerView.LayoutManager {
        return LinearLayoutManager(context!!, LinearLayoutManager.VERTICAL, false)
    }

    // @CallSuper 意思是不能覆盖我这里边的逻辑
    @CallSuper
    override fun onRefresh() {
        if (fragmentRecyclerView?.isLoadingMore() == true) {
            // 正处于分页
            // 踩坑：如果先执行上拉分页，然后快速返回往下拉，松手。会出现一个bug：转圈圈停止不动了
            // 原因：是立刻调用fragmentRefreshLayout?.refreshFinished() ， refreshHeader底部bottom值超过它的height
            // refreshLaout#recove方法中判定如果传递dis大于header的height，只能回复到刷新位置，不能回复到初始位置，
            // 添加了延迟后，会 等待松手的动画完成，之后去recove，就能恢复到最初状态了。
//            fragmentRefreshLayout?.refreshFinished()
            fragmentRefreshLayout?.post {
                fragmentRefreshLayout?.refreshFinished()
            }
            return
        }
        pageIndex = 1
    }

    fun finishRefresh(dataItems: List<HiDataItem<*, RecyclerView.ViewHolder>>?) {
        val success = dataItems != null && dataItems.isNotEmpty()
        // 这么判断还不够，因为可能会出现 下拉时候 又执行了上拉分页
        val refresh  = pageIndex == 1
        if (refresh) {
            fragmentLoadingProgressBar!!.visibility = View.GONE
            fragmentRefreshLayout?.refreshFinished()
            if (success) {
                fragmentEmptyView!!.visibility = View.GONE
                hiAdapter?.clearItems()
                hiAdapter?.addItems((dataItems)!!, true)
            } else {
                // 如果刷新失败，需要判断列表上是否已经有数据。若没有显示空页面状态
                if (hiAdapter!!.itemCount < 1) {
                    fragmentEmptyView!!.visibility = View.VISIBLE
                }
            }
        } else {
            // 分页
            if (success) {
                hiAdapter!!.addItems((dataItems)!!, true)
            }
            fragmentRecyclerView?.loadingFinished(success)
        }
    }

    override fun enableRefresh(): Boolean {
        return true
    }

    fun enableLoadMore(callback: ()->Unit){
        fragmentRecyclerView?.enableLoadMore(PREFETCH_SIZE) {
            if (refreshHeaderView.state == HiOverView.HiRefreshState.STATE_REFRESH) {
                // 正处于刷新状态
                fragmentRecyclerView?.loadingFinished(false)
                return@enableLoadMore
            }
            pageIndex++
            callback()
        }
    }

    fun disableLoadMore(){
        fragmentRecyclerView?.disableLoadMore()

    }

}